package com.visikard.ui.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.visikard.application.UIMessage;
import com.visikard.application.VKRegApplication;
import com.visikard.asynctask.BaseTask;
import com.visikard.asynctask.GetAudienceFriendsTask;
import com.visikard.item.ItemAudienceKard;
import com.visikard.ui.R;
import com.visikard.ui.adapter.ChooseAudienceAdapter;

public class ChooseAudienceKard extends BaseLoginCompleteActivity implements OnClickListener, OnTouchListener {

	public static final String MSG_RESULT_LIST_FKUSER = "ResultListFkUser";
	public static final String MSG_RESULT_LIST_FKKARD = "ResultListFkKard";

	public static final int FOR_RESULT_KEY = 0x12;

	private RelativeLayout lyContent;

	private ListView listFriend;
	private EditText edtSearch;

	private ArrayList<ItemAudienceKard> listAudience = new ArrayList<ItemAudienceKard>();

	private ChooseAudienceAdapter audienceAdapter;

	private String listfkUser = "";
	private String listfkKard = "";

	private BaseTask mAsyncTask;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.choose_audience_kard);
		innitControl();
		getListAudience();
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	private void innitControl() {
		lyContent = (RelativeLayout) findViewById(R.id.layout_content);
		listFriend = (ListView) findViewById(R.id.choose_audience_list);

		edtSearch = (EditText) findViewById(R.id.choose_audience_search);
		edtSearch.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				audienceAdapter.getFilter().filter(s.toString());
			}
		});
		lyContent.setOnTouchListener(this);
		listFriend.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				hideKeyBoard();
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
			}
		});

		hideKeyBoard();
	}

	// function to check atleast one kard had selected to create group.
	private boolean isFriendSelected() {
		return false;
	}

	// function to collect kards selected invite to group
	private ArrayList<String> collectFriendSelected() {
		ArrayList<String> result = new ArrayList<String>();
		return result;
	}

	public void hideKeyBoard() {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(lyContent.getWindowToken(), 0);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		hideKeyBoard();
		return false;
	}

	public interface onItemCallBack {
		public void onItemClick();
	}

	private void getListAudience() {
		mAsyncTask = new GetAudienceFriendsTask(mHandler);
		mAsyncTask.execute(VKRegApplication.uid, VKRegApplication.loginSession);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.choose_audience_back:
				hideKeyBoard();
				finish();
				break;
			case R.id.choose_audience_ok:
				hideKeyBoard();
				if (audienceAdapter != null && audienceAdapter.getCount() > 0) {
					listfkUser = audienceAdapter.getListUser();
					listfkKard = audienceAdapter.getListFkKard();
				}
				Intent data = new Intent();
				data.putExtra(MSG_RESULT_LIST_FKUSER, listfkUser);
				data.putExtra(MSG_RESULT_LIST_FKKARD, listfkKard);
				setResult(Activity.RESULT_OK, data);
				finish();
				break;
			default:
				break;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
			case UIMessage.MSG_GET_LIST_SUCCESS:
				listAudience = (ArrayList<ItemAudienceKard>) msg.obj;
				if (listAudience == null) {
					listAudience = new ArrayList<ItemAudienceKard>();
				}
				if (listAudience != null && listAudience.size() > 0)
					listFriend.setVisibility(View.VISIBLE);
				else
					listFriend.setVisibility(View.GONE);

				if (audienceAdapter == null) {
					audienceAdapter = new ChooseAudienceAdapter(ChooseAudienceKard.this, listAudience);
					listFriend.setAdapter(audienceAdapter);
				}
				else
					audienceAdapter.notifyDataSetChanged();
				return true;
		}
		return super.handleMessage(msg);
	}
}
